package de.openkanban.client.ui.footer;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.BkgndRepeat;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;

/**
 * GUI Element of the FooterPanel.
 */
public class FooterPanel extends HLayout {

	private boolean opened = false;

	private Layout footerLayout;

	public FooterPanel(Layout footerLayout) {
		this.footerLayout = footerLayout;

		Img gripImg = new Img("grip.png");
		gripImg.setWidth(30);
		gripImg.setHeight(18);

		setHeight(20);
		setAlign(Alignment.CENTER);
		setBackgroundImage("admin_panel_bg.png");
		setBackgroundRepeat(BkgndRepeat.REPEAT_X);
		addClickHandler(footerPanelClickHandler());
		addMember(gripImg);
	}

	private ClickHandler footerPanelClickHandler() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				switchFooterOpenClose();
			}
		};
	}

	/**
	 * Can be called from outside: RootPanel
	 */
	public void openFooter(AnimationCallback animationCallback) {
		if (!opened) {
			setBackgroundImage("footer_panel.png");
			footerLayout.animateShow(AnimationEffect.WIPE, animationCallback, 100);
			opened = !opened;
		}
	}

	public void closeFooter(AnimationCallback animationCallback) {
		setBackgroundImage("admin_panel_bg.png");
		footerLayout.animateHide(AnimationEffect.WIPE, animationCallback, 100);
		opened = !opened;
	}

	private void switchFooterOpenClose() {
		if (opened) {
			closeFooter(null);
		} else {
			openFooter(null);
		}
	}

	public boolean isOpened() {
		return opened;
	}

}
